package com.yc.supermarket.service.impl;

import com.yc.supermarket.entity.PurchaseInventory;
import com.yc.supermarket.entity.Sales;
import com.yc.supermarket.entity.Transaction;
import com.yc.supermarket.mapper.GoodsInfoMapper;
import com.yc.supermarket.mapper.PurchaseInventoryMapper;
import com.yc.supermarket.mapper.TransactionMapper;
import com.yc.supermarket.service.TransactionService;
import com.yc.supermarket.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TransactionServiceImpl implements TransactionService {

    private TransactionMapper mapper;

    @Override
    public Map<String, Object> findByStatus(int status) {
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(TransactionMapper.class);
        List<Transaction> list = mapper.findByStatus(status);
        Map<String, Object> m = new HashMap<>();
        if (null == list ) {
            m.put("code", 600);
            m.put("msg", "暂无数据");
        } else {
            m.put("code", 200);
            m.put("msg", "成功");
            m.put("data", list);
        }
        MybatisUtil.close(session);
        return m;
    }

    @Override
    public Map<String, Object> findByTrem(Map<String,Object> map) {
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(TransactionMapper.class);
        List<Transaction> list = mapper.findByTrem(map);
        Map<String, Object> m = new HashMap<>();
        if (null == list ) {
            m.put("code", 600);
            m.put("msg", "暂无数据");
        } else {
            m.put("code", 200);
            m.put("msg", "成功");
            m.put("data", list);
        }
        MybatisUtil.close(session);
        return m;
    }

    @Override
    public Map<String, Object> findByUserId(Integer userId) {
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(TransactionMapper.class);
        List<Sales> list = mapper.findByUserId(userId);
        Map<String, Object> m = new HashMap<>();
        if (null == list ) {
            m.put("code", 600);
            m.put("msg", "暂无数据");
        } else {
            m.put("code", 200);
            m.put("msg", "成功");
            m.put("data", list);
        }
        MybatisUtil.close(session);
        return m;
    }

    @Override
    public boolean modifyStatus(String transactionId) {
        SqlSession session = MybatisUtil.getSession(true);
        mapper = session.getMapper(TransactionMapper.class);
        boolean result = mapper.modifyStatus(transactionId);
        MybatisUtil.close(session);
        return result;
    }
}
